package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.javabean.UsersBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.UsersModel;

public class ModifyPersonalActivity extends BaseActivity implements RetrofitListener {

    private Context mContext;

    private int u_id;
    private String u_personal;
    private EditText upersonal;

    private ImageView left;

    @Override
    void initViews() {
        setLayout(R.layout.activity_modify_personal);

        upersonal = (EditText) findViewById(R.id.u_personal);

      //  button_modify_info = (Button) findViewById(R.id.button_modify_info);

        left = (ImageView) findViewById(R.id.left_button);

    }

    @Override
    void initData() {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sharedPreferences.getInt("u_id", 0);

       // upersonal=getIntent().getStringExtra("u_personal",-1);
//        Bundle bundle = new Bundle();
//        bundle.putString("u_personal", u_personal);

     //   upersonal.setText("u_personal");

        getUserInfo();
    }

    void getUserInfo() {
        UsersModel usersModel = new UsersModel();
        usersModel.getUserInfo(u_id, this);
    }

    void updateuserpersonal() {
        UsersModel usersModel = new UsersModel();
        usersModel.updateuserpersonal(
                u_id,
                upersonal.getText().toString(),this);

    }

    @Override
    void initEvents() {
//        left.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });


        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateuserpersonal();
            }
        });

    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag) {
            case Constants.GETUSERINFO:
                UsersBean usersBean = (UsersBean) o;
                upersonal.setText(usersBean.getU_personal());
                break;

            case Constants.UPDATEUSERPERSONAL:
                ResultBean resultBean=(ResultBean) o;
                if ("1".equals(resultBean.getSuccess())) {
                    Toast.makeText(ModifyPersonalActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                    finish();

                }else{
                    Toast.makeText(ModifyPersonalActivity.this, "失败", Toast.LENGTH_LONG).show();
                    finish();
                }
                break;

                }
    }
        @Override
        public void onFailed () {

        }

}